package org.example;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Copyright (c) 2023-2033 All Rights Reserved
 *
 * 1. Condition 对象由 lock 对象创建，在调用前必须先获取锁
 * 2. Condition 主要通过 await() signal() 方法实现精确通知
 *
 * @date: 2023/7/11
 */
public class ConditionTest {
    private static int status = 1;

    private static Lock lock = new ReentrantLock();
    private static Condition condition0 = lock.newCondition();
    private static Condition condition1 = lock.newCondition();
    private static Condition condition2 = lock.newCondition();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(4);

        executorService.execute(() -> {
            lock.lock();
            try {
                while (status != 0) {
                    System.out.println("status = " + status + " condition0.await()");
                    condition0.await();
                }
                for (int i = 0; i < 5; i++) {
                    System.out.println(Thread.currentThread().getName() + " condition0");
                }
                status = 2;
                condition2.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("condition0 lock.unlock()");
                lock.unlock();
            }
        });

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                try {
                    while (status != 1) {
                        System.out.println("status = " + status + " condition1.await()");
                        condition1.await();
                    }
                    for (int i = 0; i < 5; i++) {
                        System.out.println(Thread.currentThread().getName() + " condition1");
                    }
                    status = 0;
                    condition0.signal();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("condition1 lock.unlock()");
                    lock.unlock();
                }
            }
        });

        executorService.execute(() -> {
            lock.lock();
            try {
                while (status != 2) {
                    System.out.println("status = " + status + " condition2.await()");
                    condition2.await();
                }
                for (int i = 0; i < 5; i++) {
                    System.out.println(Thread.currentThread().getName() + " condition2");
                }
                status = 0;
                condition1.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("condition2 lock.unlock()");
                lock.unlock();
            }
        });

        executorService.shutdown();
    }
}
